package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallGoodsMapper;
import org.linlinjava.litemall.db.dao.LitemallMechanismGoodsMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.domain.LitemallGoods.Column;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LitemallMechanismGoodsService {
    @Resource
    private LitemallMechanismGoodsMapper mechanismGoodsMapper;

    public LitemallMechanismGoods findById(Integer id) {
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return mechanismGoodsMapper.selectOneByExampleWithBLOBs(example);
    }

    public void updateById(LitemallMechanismGoods goods) {
        mechanismGoodsMapper.updateByPrimaryKeySelective(goods);
    }

    public void updateById1(LitemallMechanismGoods goods) {
        mechanismGoodsMapper.updateByPrimaryKeySelective1(goods);
    }

    public LitemallMechanismGoods findEditById(Integer id) {
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return mechanismGoodsMapper.selectOneByExampleWithBLOBs(example);
    }

    public List<LitemallMechanismGoods> querySelective(String goodsSn, String name, Integer mechanismId, Integer page, Integer size, String sort, String order) {
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        LitemallMechanismGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(goodsSn)){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andMechanismIdEqualTo(mechanismId);
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, size);
        return mechanismGoodsMapper.selectByExampleWithBLOBs(example);
    }

    public List<LitemallMechanismGoods> queryTotalSelective(String goodsSn, String name, Integer mechanismId, String sort, String order) {
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        LitemallMechanismGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(goodsSn)){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andMechanismIdEqualTo(mechanismId);
        criteria.andDeletedEqualTo(false);
        return mechanismGoodsMapper.selectByExampleWithBLOBs(example);
    }

    public int countSelective(String goodsSn, String name, Integer mechanismId, Integer page, Integer size, String sort, String order) {
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        LitemallMechanismGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(goodsSn)){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andMechanismIdEqualTo(mechanismId);
        criteria.andDeletedEqualTo(false);
        return (int)mechanismGoodsMapper.countByExample(example);
    }

    public void deleteById(Integer id) {
        mechanismGoodsMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallMechanismGoods goods) {
        mechanismGoodsMapper.insertSelective(goods);
    }

    public boolean checkExistByName(String name,Integer mechanismId) {
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        example.or().andMechanismIdEqualTo(mechanismId).andNameEqualTo(name).andDeletedEqualTo(false);
        return mechanismGoodsMapper.countByExample(example) != 0;
    }

    public List<LitemallMechanismGoods> getAllGoods(List<Integer> categoryIds, LocalDateTime time){
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        example.or().andMechanismIdIn(categoryIds).andIsCheckEqualTo((byte)2).andDiscountsEndTimeGreaterThan(time);
        PageHelper.startPage(1, 5);
        return mechanismGoodsMapper.selectByExampleWithBLOBs(example);
    }
    public List<LitemallMechanismGoods> queryTop3(String longitude,String latitude){
        return mechanismGoodsMapper.selectTop3(longitude,latitude);
    }

    public LitemallMechanismGoodsVo showDetail(Integer id){
        return mechanismGoodsMapper.showVoucherDetail(id);
    }
    public  List<Map> getGoodsByMechanismId(Integer id){
        LitemallMechanismGoodsExample example = new LitemallMechanismGoodsExample();
        example.or().andMechanismIdEqualTo(id).andDeletedEqualTo(false).andDiscountsEndTimeGreaterThanOrEqualTo(LocalDateTime.now()).andIsCheckEqualTo((byte)2);
        return mechanismGoodsMapper.selectByMechanismId(id);
    }

    public void updateAllByIds(Integer types,List<String> goodIds){
        mechanismGoodsMapper.updateAllByIds(types,goodIds);
    }
}
